<?php
define ('CORE_APP_DIR', 'ypCore');
define ('APP_DIR', 'apps');
define ('APP_VIEWS_DIR', APP_DIR . '/views');
define ('APP_RENDER_DIR', APP_DIR . '/render');
define ('APP_CONTROLLER_DIR', APP_DIR . '/controller');
define ('CORE_MODULES_DIR', CORE_APP_DIR . '/modules');
define ('CORE_FUNCTION_DIR', CORE_APP_DIR . '/functions');
define ('CORE_CLASS_DIR', CORE_APP_DIR . '/class');
define ('CORE_MODULE_CLASS_DIR', CORE_APP_DIR . '/modules');
define ('CORE_LAYOUT_DIR', ROOT . '/layout');
define ('SETTING_FILE', ROOT . '/' . APP_DIR . '/' . 'settings.phsp');

new __CONFIG;

class __AUTOLOADER {
	private static $_instance;
	protected $_rootDir = '.';
	protected $_setup = false;

	public function setupAutoloader($rootDir) {
		if ($this->_setup) {
			return;
		}

		$this->_rootDir = $rootDir;
		$this->_setupAutoloader();

		$this->_setup = true;
	}

	protected function _setupAutoloader() {
		if (@ini_get('open_basedir')) {
			set_include_path($this->_rootDir . PATH_SEPARATOR . '.');
		} else {
			set_include_path($this->_rootDir . PATH_SEPARATOR . '.' . PATH_SEPARATOR . get_include_path());
		}

		spl_autoload_register(array($this, 'autoload'));
	}

	public function autoload($class)
	{
		if (class_exists($class, false) || interface_exists($class, false)) {
			return true;
		}

		if ($class == 'utf8_entity_decoder') {
			return true;
		}

		$filename = $this->autoloaderClassToFile($class);
		if (!$filename) {
			return false;
		}

		if (file_exists($filename)) {
			include($filename);
			return (class_exists($class, false) || interface_exists($class, false));
		}

		return false;
	}

	public function autoloaderClassToFile($className) {
		if (preg_match('#[^a-zA-Z0-9_]#', $className)) {
			return false;
		}

		if (substr($className, 0, 2) == '__') {
			$fileName = CORE_APP_DIR . '/class/' . strtolower(substr($className, 2)) . '.php';
		}
		elseif (substr($className, 0, 9) == 'Exception') {
			$fileName = CORE_APP_DIR . '/class/Exception/' . strtolower(substr($className, 10)) . '.php';
		}
		elseif (substr($className, 0, 4) == 'view') {
			$class = substr($className, 4);
			$fileName = APP_VIEWS_DIR . '/' . str_replace('_', '/', $class) . '.php';
		}
		elseif (substr($className, 0, 11) == 'controller_') {
			$class = substr($className, 11);
			$fileName = APP_CONTROLLER_DIR . '/' . str_replace('_', '/', $class) . '.php';
		}
		elseif (substr($className, 0, 7) == 'render_') {
			$class = substr($className, 7);
			$fileName = APP_RENDER_DIR . '/' . str_replace('_', '/', $class) . '.php';
		}
		elseif (substr($className, 0, 7) == 'module_') {
			$fileName = CORE_CLASS_DIR . '/' . 'modules' . '/' . substr($className, 8) . '/' . (substr($className, 8));
		}

		if (empty($fileName)) return false;

		return $this->_rootDir . '/' . $fileName;
	}

	public function getRootDir()
	{
		return $this->_rootDir;
	}

	public static final function getInstance()
	{
		if (!self::$_instance) {
			self::$_instance = new self();
		}

		return self::$_instance;
	}

	public static function setInstance($loader = null)
	{
		self::$_instance = $loader;
	}
}


class __CONFIG {
	public function __construct() {
		if (!function_exists('ini_set')) return false;

		ini_set('magic_quotes_runtime', 'Off');
		if (!isset($_SESSION)) {
			ini_set('session.save_handler', 'files');
			ini_set('session.use_trans_sid', 0);
			ini_set('session.auto_start', 0);
			ini_set('session.use_cookies', 1);
			ini_set('session.use_only_cookies', 1);
			ini_set('session.cookie_httponly', 1);
			ini_set('session.gc_probability', 1); 
			ini_set('session.gc_divisor', 1000); //gc_probability / gc_divisor = phan tram (phan nghin) kha nang chay Garbage Collection
			ini_set('session.gc_maxlifetime', 3600); //thoi gian sau khi het han phien lam viec de Garbage Collection tien hanh xoa, 60 phut
		}

		ini_set('allow_url_fopen', 1);
		ini_set('magic_quotes_sybase', 'Off');
		
	}
}